<?php

namespace Common\Logic;

/**
 * Description of UserLoginLogic.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-11
 */
class InterfaceServerLogic extends \Common\Controller\BaseController {

    /**
     * 获取服务接口数据
     * @modify wenhui 2017-11-13
     * @param string $hotel 客栈id
     * @return array
     */
    public function getInterfaceServerInfoByHotel($hotel) {
        // 调用RPC接口登录,获取服务接口数据
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_INTERFACESERVER_URL'));
        $client->setEncryptMode(3);

        $data = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'hotel' => $hotel,
        );

        $result = $client->getInterfaceServerInfo($data);
        if ($result['httpstatus'] == 200) {
            return $result['data'];
        }

        return array();
    }

    /**
     * 获取服务接口数据
     * @modify wenhui 2017-11-13
     * @param string $hotel 客栈id
     * @param string $url 回调 url
     * @param string $serverId 接口服务id
     * @return array
     */
    public function addInterfaceServer($hotel, $url, $serverId) {
        $hotelDetailModel = D('HotelDetail');

        // 获取客栈与微预定数据
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelInfo = $hotelLogic->getHotelByHotelId($hotel);
        $hotelDetail = $hotelDetailModel->getInfoByHotel($hotel);

        $hotel_status = $hotelInfo[0]['status'];
        $hotel_usage = $hotelInfo[0]['usage'];
        $hotel_enddate = $hotelInfo[0]['enddate'];
        $bk_hotel_opened = $hotelDetail['opened'];
        $bk_hotel_enddate = $hotelDetail['enddate'];
 
        // 生成 mz_appid
        $mz_appid = substr(md5($hotel . '_' . $serverId . C('MEIZHU_ENCRYPT_SALT')), 2, 14) . 'mz';
        // 生成 mz_key
        $mz_key = $this->makeMzKey();

        // 调用RPC接口登录,获取服务接口数据
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_INTERFACESERVER_URL'));
        $client->setEncryptMode(3);

        $data = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'hotel' => $hotel,
            'url' => $url,
            'mz_appid' => $mz_appid,
            'mz_key' => $mz_key,
            'hotel_status' => $hotel_status,
            'hotel_usage' => $hotel_usage,
            'hotel_enddate' => $hotel_enddate,
            'bk_hotel_opened' => $bk_hotel_opened,
            'bk_hotel_enddate' => $bk_hotel_enddate,
            'server_id' => $serverId,
        );

        $result = $client->addInterfaceServer($data);
        if ($result['httpstatus'] == 200) {
            return true;
        }

        return false;
    }

    /**
     * 获取服务接口数据
     * @modify wenhui 2017-11-13
     * @param string $hotel 客栈id
     * @param string $serverId
     * @param string $url
     * @param string $token
     * @return array
     */
    public function saveInterfaceServer($hotel, $serverId = null, $url = null, $token = null, $hotel_status = null) {

        // 调用RPC接口登录,获取服务接口数据
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_INTERFACESERVER_URL'));
        $client->setEncryptMode(3);

        $data = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'hotel' => $hotel,
        );

        if (!is_null($serverId)) {
            $data['server_id'] = $serverId;
        }
        if (!is_null($url)) {
            $data['url'] = $url;
        }
        if (!is_null($token)) {
            // 生成 mz_key
            $mz_key = $this->makeMzKey();
            $data['mz_key'] = $mz_key;
        }
        if (!is_null($hotel_status)) {
            $data['hotel_status'] = $hotel_status;
        }

        $result = $client->saveInterfaceServer($data);

        if ($result['httpstatus'] == 200) {
            if (isset($mz_key)) {
                return $mz_key;
            } else {
                return true;
            }
        }

        return false;
    }

    private function makeMzKey() {
        $string = getRandomCode(6, 0);
        $mz_key = md5('mz' . encrypt($string, 'E', C('MEIZHU_ENCRYPT_SALT')));
        $mz_key = substr($mz_key, 0, 30) . 'mz';

        return $mz_key;
    }
}
